package com.tzinfo.tzb.view.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tzinfo.data.SubjectResultData;
import com.tzinfo.tzb.PostActivity;
import com.tzinfo.tzt.R;

import java.util.List;

/**
 * Created by Administrator on 2014/11/21.
 */
public class SubjecttAdapter extends BaseAdapter{

    private Context context;
    private List<SubjectResultData> data;
    ImageLoader imageLoader;
    SubjectInterface subjectInterface;
    private  boolean aBoolean;
//    private int fid;
//    private int tid;

    public SubjecttAdapter(Context context, List<SubjectResultData> data,boolean b) {
        this.context = context;
        this.data = data;
        imageLoader = ImageLoader.getInstance();
        subjectInterface = (SubjectInterface)context;
        aBoolean = b;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_subject_adapter, parent, false);
            holder.tv_subject = (TextView) convertView.findViewById(R.id.item_subject_adapter_tv_subject);
            holder.tv_views = (TextView) convertView.findViewById(R.id.item_subject_adapter_tv_views);
            holder.tv_dateline = (TextView) convertView.findViewById(R.id.item_subject_adapter_tv_dateline);
            holder.tv_replies = (TextView) convertView.findViewById(R.id.item_subject_adapter_tv_replies);
            holder.ll = (LinearLayout) convertView.findViewById(R.id.item_subject_adapter_ll);
            holder.iv_avatar = (ImageView) convertView.findViewById(R.id.item_subject_adapter_iv_avatar);
            holder.doll = (LinearLayout) convertView.findViewById(R.id.item_subject_adapter_doll);
            holder.tv_del = (TextView) convertView.findViewById(R.id.item_subject_adapter_tv_del);
            holder.tv_pass = (TextView) convertView.findViewById(R.id.item_subject_adapter_tv_pass);
            holder.tv_digest=(TextView)convertView.findViewById(R.id.item_subject_adapter_tv_digest);

            holder.fid = data.get(position).getFid();
            holder.tid = data.get(position).getTid();
            //    holder.tv_close

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        if (aBoolean){
            holder.tv_pass.setVisibility(View.VISIBLE);
        }
        else holder.tv_pass.setVisibility(View.GONE);
        holder.tv_subject.setText(data.get(position).getSubject());
        holder.tv_replies.setText(String.valueOf(data.get(position).getReplies()));
        holder.tv_views.setText(String.valueOf(data.get(position).getViews()));
        holder.tv_dateline.setText(data.get(position).getDateline().toString().split(" ")[0].split("/")[2]+"" +
                "/"+data.get(position).getDateline().toString().split(" ")[0].split("/")[0]+"/"+data.get(position).getDateline().toString().split(" ")[0].split("/")[1]);
        final ViewHolder hh = holder;
        holder.tv_del.setOnClickListener(new View.OnClickListener() {
            //调用接口处理主题
            @Override
            public void onClick(View v) {
                subjectInterface.delete(data.get(position).getFid(),data.get(position).getTid(),"Del",position);
                hh.doll.setVisibility(View.GONE);
            }
        });
        holder.tv_pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                subjectInterface.pass(data.get(position).getFid(),data.get(position).getTid(),"Pass",position);
                hh.doll.setVisibility(View.GONE);
            }
        });

        holder.tv_digest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new AlertDialog.Builder(context)
                        .setItems(new String[]{"精华1", "精华2", "精华3"},
                                new DialogInterface.OnClickListener() {
                                    int lv;
                                    public void onClick(DialogInterface dialog, int which) {
                                        switch (which) {
                                            case 0:
                                                lv =1;
                                                break;
                                            case 1:
                                                lv =2;
                                                break;
                                            case 2:
                                                lv =3;
                                                break;
                                        }
                                        subjectInterface.digest(data.get(position).getFid(),data.get(position).getTid(),lv);
                                        dialog.dismiss();
                                        hh.doll.setVisibility(View.GONE);
                                    }
                                }
                        )
                        .show();
            }
        });

        //对主题的操作
        final ViewHolder h = holder;
        holder.ll.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                return false;
            }

        });
        holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PostActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("fid", data.get(position).getFid());
                bundle.putInt("tid", data.get(position).getTid());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        holder.ll.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                if (h.doll.getVisibility() == View.GONE) {
                    h.doll.setVisibility(View.VISIBLE);
                } else h.doll.setVisibility(View.GONE);
                return true;
            }
        });
        if (data.get(position).getUserinfo() != null)
            imageLoader.displayImage(data.get(position).getUserinfo().getAvatar(), holder.iv_avatar);
        return convertView;
    }



    public class ViewHolder {
        TextView tv_subject;
        TextView tv_views;
        TextView tv_dateline;
        TextView tv_replies;
        TextView tv_del;
        TextView tv_pass;
        TextView tv_digest;
        ImageView iv_avatar;
        LinearLayout doll;
        LinearLayout ll;
        int fid;
        int tid;
    }

    public interface SubjectInterface {
        public void delete(int a , int b,String s,int position);
        public void pass(int a , int b,String s,int position);
        public void digest(int a , int b,int type);
    }




}
